home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / quickdraw fx / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  12.0 KB  |  522 lines

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Edgar Lee    
  7.  
  8.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/13/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "FX.h"
  25. #include <Fonts.h>
  26. #include <TextEdit.h>
  27. #include <Dialogs.h>
  28. #include <Resources.h>
  29. #include <TextUtils.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. /* Global Variable Definitions */
  34.  
  35. WindowPtr        gWindow;
  36. GWorldPtr        gGWorld = nil;
  37. int                gCurrentExample = 11;
  38.  
  39. itemType    tItem[numTItems];
  40. itemType    aItem[numAItems];
  41. itemType    cItem[numCItems];
  42. itemType    dItem[numDItems];
  43. itemType    mItem[numMItems];
  44. itemType    bItem[numBItems];
  45. itemType    pItem[numPItems];
  46. itemType    lItem[numLItems];
  47.  
  48. itemSettings    settings;
  49.  
  50. void main()
  51. {
  52.     MaxApplZone();
  53.  
  54.     initMac();
  55.     
  56.     createOffscreen( 1 );
  57.     createWindow();
  58.  
  59.     resetItems();
  60.     defineItems();
  61.  
  62.     eventLoop();
  63. }
  64.  
  65. void initMac()
  66. {
  67.     InitGraf( &qd.thePort );
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     TEInit();
  72.     InitDialogs( nil );
  73.     InitCursor();
  74.     FlushEvents( 0, everyEvent );
  75.     
  76.     setUpMenus();
  77. }
  78.  
  79. void createOffscreen( pictItem )
  80. int        pictItem;
  81. {
  82.     PicHandle    pict;
  83.     Rect        rect;
  84.     CGrafPtr    currentPort;
  85.     GDHandle    currentDevice;
  86.     
  87.     if (gGWorld != nil)
  88.         DisposeGWorld( gGWorld );
  89.     
  90.     pict = (PicHandle)GetResource( 'PICT', pictItem + 127 );
  91.     
  92.     rect = (**pict).picFrame;
  93.     
  94.     GetGWorld( ¤tPort, ¤tDevice );
  95.     NewGWorld( &gGWorld, 32, &rect, nil, nil, 0 );
  96.         
  97.     LockPixels( (*gGWorld).portPixMap );
  98.     SetGWorld( gGWorld, nil );
  99.  
  100.     DrawPicture( pict, &rect );
  101.     
  102.     SetGWorld( currentPort, currentDevice );
  103.     
  104.     ReleaseResource( (Handle)pict );
  105. }
  106.  
  107. void createWindow()
  108. {
  109.     int        width, height;
  110.     int        left, top;
  111.     Rect    rect;
  112.     
  113.     width = 58 + ((*gGWorld).portRect.right - (*gGWorld).portRect.left) * 2;
  114.     height = 230 + (*gGWorld).portRect.bottom - (*gGWorld).portRect.top;
  115.     
  116.     left = (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - width) / 2);
  117.     top = (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - height) / 2) + 20;
  118.  
  119.     SetRect( &rect, left, top, left + width, top + height );
  120.     gWindow = NewCWindow( 0L, &rect, "\pQuickDraw Special Effects", true, noGrowDocProc,
  121.                             (WindowPtr)-1L, true, 0 );
  122.                             
  123.     SetPort( gWindow );
  124. }
  125.  
  126. void resetItems()
  127. {
  128.     settings.tItem = 1;
  129.     settings.aItem = 1;
  130.     settings.cItem = 1;
  131.     settings.mItem = 1;
  132.     settings.dItem = 1;
  133.     settings.pItem = 1;
  134.     settings.lItem = 1;
  135. }
  136.  
  137. void defineItems()
  138. {
  139.     int        i;
  140.     int        col, row;
  141.     
  142.     char    tNames[numTItems][15] = {    "srcCopy", "srcOr", "srcXor", "srcBic",
  143.                                         "notSrcCopy", "notSrcOr", "notSrcXor", "notSrcBic"    };
  144.     char    aNames[numAItems][15] = {    "blend", "addPin", "addOver", "subPin",
  145.                                         "adMax", "subMin", "adMin", "transparent"    };
  146.     char    cNames[numCItems][15] = {    "None", "Inverse", "FG Only", "BG Only"    };
  147.     char    dNames[numDItems][15] = {    "Dithering OFF", "Dithering ON"    };
  148.     char    mNames[numMItems][15] = {    "None", "Search Proc", "ColorTable"    };
  149.     char    bNames[numBItems][15] = {    "White", "Black", "White&Black"    };
  150.     char    pNames[numPItems][15] = {    "SeedCFill", "SeedFill"    };
  151.     char    lNames[numLItems][15] = {    "CalcCMask", "CalcMask"    };
  152.     
  153.     settings.bItem = 1;
  154.     
  155.     col = 15;
  156.     row = (*gWindow).portRect.bottom - 157;
  157.     
  158.     for (i = 0; i < numTItems; i++)
  159.     {
  160.         strcpy(P2CStr(tItem[i].label),tNames[i]);
  161.         C2PStr((char *)tItem[i].label);
  162.         SetRect( &tItem[i].rect, col + 40, row + (i * 15),
  163.                     col + 40 + 90, row + ((i + 1) * 15) );
  164.     }
  165.     
  166.     for (i = 0; i < numAItems; i++)
  167.     {
  168.         strcpy(P2CStr(aItem[i].label),aNames[i]);
  169.         C2PStr((char *)aItem[i].label);
  170.         SetRect( &aItem[i].rect, col + 140, row + (i * 15),
  171.                     col + 140 + 90, row + ((i + 1) * 15) );
  172.     }
  173.     
  174.     for (i = 0; i < numCItems; i++)
  175.     {
  176.         strcpy(P2CStr(cItem[i].label),cNames[i]);
  177.         C2PStr((char *)cItem[i].label);
  178.         SetRect( &cItem[i].rect, col + 240, row + (i * 15),
  179.                     col + 240 + 90, row + ((i + 1) * 15) );
  180.     }
  181.     
  182.     for (i = 0; i < numDItems; i++)
  183.     {
  184.         strcpy(P2CStr(dItem[i].label),dNames[i]);
  185.         C2PStr((char *)dItem[i].label);
  186.         SetRect( &dItem[i].rect, col + 240, row + 90 + (i * 15),
  187.                     col + 240 + 90, row + 90 + ((i + 1) * 15) );
  188.     }
  189.         
  190.     for (i = 0; i < numMItems; i++)
  191.     {
  192.         strcpy(P2CStr(mItem[i].label),mNames[i]);
  193.         C2PStr((char *)mItem[i].label);
  194.         SetRect( &mItem[i].rect, col + 340, row + (i * 15),
  195.                     col + 340 + 90, row + ((i + 1) * 15) );
  196.     }
  197.     
  198.     for (i = 0; i < numBItems; i++)
  199.     {
  200.         strcpy(P2CStr(bItem[i].label),bNames[i]);
  201.         C2PStr((char *)bItem[i].label);
  202.         SetRect( &bItem[i].rect, col + 340, row + 75 + (i * 15),
  203.                     col + 340 + 90, row + 75 + ((i + 1) * 15) );
  204.     }
  205.     
  206.     for (i = 0; i < numPItems; i++)
  207.     {
  208.         strcpy(P2CStr(pItem[i].label),pNames[i]);
  209.         C2PStr((char *)pItem[i].label);
  210.         SetRect( &pItem[i].rect, col + 440, row + (i * 15),
  211.                     col + 440 + 90, row + ((i + 1) * 15) );
  212.     }
  213.     
  214.     for (i = 0; i < numLItems; i++)
  215.     {
  216.         strcpy(P2CStr(lItem[i].label),lNames[i]);
  217.         C2PStr((char *)lItem[i].label);
  218.         SetRect( &lItem[i].rect, col + 440, row + 60 + (i * 15),
  219.                     col + 440 + 90, row + 60 + ((i + 1) * 15) );
  220.     }
  221. }
  222.  
  223. void updateWindow()
  224. {
  225.     long    ticks;
  226.     
  227.     drawBackground();
  228.     drawAllItems();
  229.     drawExampleName();
  230.     drawSourceImage();
  231.     
  232.     ticks = drawFXImage();
  233.     drawTime( ticks );
  234. }
  235.  
  236. void drawBackground()
  237. {
  238.     Rect        rect;
  239.     RGBColor    color;
  240.     
  241.     color.red = color.green = color.blue = 8700;
  242.     
  243.     RGBForeColor( &color );
  244.     PaintRect( &(*gWindow).portRect );
  245.     
  246.     TextFont( kFontIDTimes );
  247.     TextMode( srcOr );
  248.     TextSize( 24 );
  249.     
  250.     drawName( 85, 22, "\pSource Image" );
  251.     drawName( (*gWindow).portRect.right - 215, 22, "\pNew Image" );
  252.     
  253.     SetRect( &rect, 15, (*gWindow).portRect.bottom - 180, 
  254.                 (*gWindow).portRect.right - 15, (*gWindow).portRect.bottom - 30 );
  255.     drawDeepBox( &rect );
  256.     
  257.     TextSize( 12 );
  258.     
  259.     drawName( tItem[0].rect.left, tItem[0].rect.top - 8, "\pTransfer Mode" );
  260.     drawName( aItem[0].rect.left, aItem[0].rect.top - 8, "\pArithmetic Mode" );
  261.     drawName( cItem[0].rect.left, cItem[0].rect.top - 8, "\pColorization" );
  262.     drawName( dItem[0].rect.left, dItem[0].rect.top - 8, "\pDither" );
  263.     drawName( mItem[0].rect.left, mItem[0].rect.top - 8, "\pColor Mapping" );
  264.     drawName( bItem[0].rect.left, bItem[0].rect.top - 8, "\pDestination" );
  265.     drawName( pItem[0].rect.left, pItem[0].rect.top - 8, "\pPaint Bucket" );
  266.     drawName( lItem[0].rect.left, lItem[0].rect.top - 8, "\pLasso Tool" );
  267. }
  268.  
  269. void drawAllItems()
  270. {
  271.     drawTransferItems();
  272.     drawArithmeticItems();
  273.     drawColorizeItems();
  274.     drawDitherItems();
  275.     drawMappingItems();
  276.     drawDestinationItems();
  277.     drawPaintBucketItems();
  278.     drawLassoToolItems();
  279. }
  280.  
  281. void drawTransferItems()
  282. {
  283.     int            i;
  284.     Boolean        listEnabled;
  285.  
  286.     listEnabled = (gCurrentExample / 10 == transferID || gCurrentExample / 10 == customID);
  287.     
  288.     for (i = 0; i < numTItems; i++)
  289.         drawItem( tItem[i].rect.left, tItem[i].rect.top, &tItem[i].label,
  290.                     listEnabled, settings.tItem == i + 1 );
  291. }
  292.  
  293. void drawArithmeticItems()
  294. {
  295.     int            i;
  296.     Boolean        listEnabled;
  297.     
  298.     listEnabled = (gCurrentExample / 10 == arithmeticID);
  299.     
  300.     for (i = 0; i < numAItems; i++)
  301.         drawItem( aItem[i].rect.left, aItem[i].rect.top, &aItem[i].label,
  302.                     listEnabled, settings.aItem == i + 1 );
  303. }
  304.  
  305. void drawColorizeItems()
  306. {
  307.     int            i;
  308.     Boolean        listEnabled;
  309.     
  310.     listEnabled = (gCurrentExample / 10 == colorizationID || gCurrentExample / 10 == customID);
  311.         
  312.     for (i = 0; i < numCItems; i++)
  313.         drawItem( cItem[i].rect.left, cItem[i].rect.top, &cItem[i].label,
  314.                     listEnabled, settings.cItem == i + 1 );
  315. }
  316.  
  317. void drawDitherItems()
  318. {
  319.     int            i;
  320.     Boolean        listEnabled;
  321.     
  322.     listEnabled = (gCurrentExample / 10 == ditherID || gCurrentExample / 10 == customID);
  323.         
  324.     for (i = 0; i < numDItems; i++)
  325.         drawItem( dItem[i].rect.left, dItem[i].rect.top, &dItem[i].label,
  326.                     listEnabled, settings.dItem == i + 1 );
  327. }
  328.  
  329. void drawMappingItems()
  330. {
  331.     int            i;
  332.     Boolean        listEnabled;
  333.     
  334.     listEnabled = (gCurrentExample / 10 == searchProcID || gCurrentExample / 10 == customID);
  335.     
  336.     for (i = 0; i < numMItems; i++)
  337.         drawItem( mItem[i].rect.left, mItem[i].rect.top, &mItem[i].label,
  338.                     listEnabled, settings.mItem == i + 1 );
  339. }
  340.  
  341. void drawDestinationItems()
  342. {
  343.     int            i;
  344.     
  345.     for (i = 0; i < numBItems; i++)
  346.         drawItem( bItem[i].rect.left, bItem[i].rect.top, &bItem[i].label,
  347.                     true, settings.bItem == i + 1 );
  348. }
  349.  
  350. void drawPaintBucketItems()
  351. {
  352.     int            i;
  353.     Boolean        listEnabled;
  354.     
  355. //    listEnabled = (gCurrentExample / 10 == paintBucketID);
  356.     listEnabled = false;
  357.     
  358.     for (i = 0; i < numPItems; i++)
  359.         drawItem( pItem[i].rect.left, pItem[i].rect.top, &pItem[i].label,
  360.                     listEnabled, settings.pItem == i + 1 );
  361. }
  362.  
  363. void drawLassoToolItems()
  364. {
  365.     int            i;
  366.     Boolean        listEnabled;
  367.     
  368. //    listEnabled = (gCurrentExample / 10 == lassoID);
  369.     listEnabled = false;
  370.     
  371.     for (i = 0; i < numLItems; i++)
  372.         drawItem( lItem[i].rect.left, lItem[i].rect.top, &lItem[i].label,
  373.                     listEnabled, settings.lItem == i + 1 );
  374. }
  375.  
  376. void drawName( left, top, name )
  377. int        left, top;
  378. Str255    name;
  379. {
  380.     RGBColor    color;
  381.     
  382.     ForeColor( blackColor );
  383.     MoveTo( left + 1, top + 1 );
  384.     DrawString( name );
  385.     
  386.     color.red = color.green = 0xffff;
  387.     color.blue = 40000;
  388.     RGBForeColor( &color );
  389.     
  390.     MoveTo( left, top );
  391.     DrawString( name );
  392. }
  393.  
  394. void drawItem( left, top, label, listEnabled, itemEnabled )
  395. int            left, top;
  396. Str255        *label;
  397. Boolean        listEnabled;
  398. Boolean        itemEnabled;
  399. {
  400.     Rect        rect;
  401.     RGBColor    color, markColor;
  402.     
  403.     if (listEnabled)
  404.         color.red = color.green = color.blue = 0xffff;
  405.     else
  406.         color.red = color.green = color.blue = 0xffff / 2;
  407.         
  408.     SetRect( &rect, left, top + 2, left + 12, top + 14 );
  409.     ForeColor( blackColor );
  410.     FrameOval( &rect );
  411.     
  412.     OffsetRect( &rect, -1, -1 );
  413.     RGBForeColor( &color );
  414.     FrameOval( &rect );
  415.                 
  416.     if (listEnabled && itemEnabled)
  417.         markColor.red = markColor.green = markColor.blue = 0xffff;
  418.     else if (itemEnabled)
  419.         markColor.red = markColor.green = markColor.blue = 0xffff / 2;
  420.     else
  421.         markColor.red = markColor.green = markColor.blue = 8700;
  422.     
  423.     InsetRect( &rect, 3, 3 );
  424.     RGBForeColor( &markColor );
  425.     PaintOval( &rect );
  426.     
  427.     ForeColor( blackColor );
  428.     MoveTo( left + 16, top + 11 );
  429.     DrawString( (unsigned char*)label );
  430.     
  431.     RGBForeColor( &color );
  432.     MoveTo( left + 15, top + 10 );
  433.     DrawString( (unsigned char*)label );
  434. }
  435.  
  436. void drawDeepBox( rect )
  437. Rect    *rect;
  438. {
  439.     Rect        box;
  440.     
  441.     box = *rect;
  442.     box.right += 2;
  443.     box.bottom += 2;
  444.     
  445.     ForeColor( blackColor );
  446.     FrameRect( &box );
  447.     
  448.     OffsetRect( &box, -1, -1 );
  449.     ForeColor( whiteColor );
  450.     FrameRect( &box );
  451. }
  452.  
  453. void drawExampleName()
  454. {
  455.     int            left, top;
  456.     Rect        rect;
  457.     Str255        label = "\pCurrent Example:  ";
  458.     Str255        name[9] = {    "\p TRANSFER MODES", "\pARITHMETIC MODES", "\p   DITHERING",
  459.                             "\p  COLORIZATION", "\p  COLOR MAPPING", "\p  PAINT BUCKET",
  460.                             "\p   LASSO TOOL", "\pPIXEL AVERAGING", "\p    CUSTOM"    };
  461.     
  462.     left = 15;
  463.     top = (*gWindow).portRect.bottom - 13;
  464.     
  465.     drawName( left, top, label );
  466.     left += StringWidth( label );
  467.     
  468.     SetRect( &rect, left - 2, top - 12, left + 130, top + 4 );
  469.     eraseRect( &rect );
  470.     
  471.     drawName( left, top, name[(gCurrentExample / 10) - 1] );
  472. }
  473.  
  474. void drawTime( ticks )
  475. long    ticks;
  476. {
  477.     Rect    rect;
  478.     int        left, top;
  479.     float    seconds;
  480.     char    cString[40];
  481.     
  482.     left = (*gWindow).portRect.right - 130;
  483.     top = (*gWindow).portRect.bottom - 13;
  484.     
  485.     SetRect( &rect, left - 2, top - 12, left + 90, top + 4 );
  486.     eraseRect( &rect );
  487.     
  488.     seconds = (float)ticks / 60.0;
  489.     sprintf( &cString[0], "Draw Time: %03.03f secs", seconds );
  490.     
  491.     drawName( left, top, C2PStr( cString ) );
  492. }
  493.  
  494. void eraseRect( rect )
  495. Rect    *rect;
  496. {
  497.     RGBColor    color;
  498.     
  499.     color.red = color.green = color.blue = 8700;
  500.     
  501.     RGBForeColor( &color );
  502.     PaintRect( rect );
  503. }
  504.  
  505. void drawSourceImage()
  506. {
  507.     Rect    rect;
  508.     Rect    outlineRect;
  509.  
  510.     SetRect( &rect, 20, 37, 20 + (*gGWorld).portRect.right,
  511.             37 + (*gGWorld).portRect.bottom );
  512.  
  513.     outlineRect = rect;
  514.     InsetRect( &outlineRect, -5, -5 );
  515.     drawDeepBox( &outlineRect );
  516.     
  517.     ForeColor( blackColor );
  518.     BackColor( whiteColor );
  519.     
  520.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  521.                 &(**(*gGWorld).portPixMap).bounds, &rect, srcCopy, nil );
  522. }